<?php
namespace app\index\controller;

use think\Controller;
use think\Cookie;
use think\Db;
use think\Session;

class Specialty extends Controller{
    public function index(){

        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";
        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }
//把后台表中的数据传到前台显示
        $list = Db::name('special')->paginate(9);
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $this->assign('li', $cc);
        $this->assign('list', $list);
        return view('specialty/index',["username"=>$name]);
    }
    public function specgwc(){

        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";
        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }

      //获取id在购物车显示该物品数据
        $get=$_GET['id'];
        $list = Db::name('special')->where('id',$get)->paginate();
        $cc = array();
        for($i=0;$i<count($list);$i++){
            $cc[$i]['img'] = explode("/",$list[$i]["img"]);
        }
        $this->assign('li', $cc);
        $this->assign('list', $list);

        return $this->fetch('specgwc',["username"=>$name]);
    }
  
    public function specgm(){
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";
        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }


        $list = Db::table('specialty')->where("park_user_id=".Cookie::get("id"))->select();
//    var_dump($list);
        $zongjia=0;
        for($i=0;$i<count($list);$i++){
            $array = Db::table('special')->where("id=".$list[$i]["special_id"])->find();
            $list[$i]['img'] = explode("/",$array["img"]);
            $list[$i]['name'] = $array["name"];
            $list[$i]['amounts'] =$array["amounts"]*$list[$i]["order_quantity"];
            $list[$i]['danjia'] =$array["amounts"] ;
            $list[$i]['shulian'] =$list[$i]["order_quantity"];
            $zongjia+= $list[$i]['amounts'];
        }

//    $this->assign('list', $list);
        return view('specgm',["zongjia"=>$zongjia,"list"=>$list,"username"=>$name] );
    }
    public function specde(){
        $get=$_GET['id'];
        if(Db::table('specialty')->where('id',$get)->delete()){
            return $this->specgm();
        }
    }
    //获取购物车的ID 商品ID 数量
    public  function car(){
        $array=Db::table("specialty")->where("park_user_id=".Cookie::get("id")." and special_id=".$_POST["special_id"])->find();
        if(empty($array)){
            if( Db::table("specialty")->
            insert(["park_user_id"=>Cookie::get("id"),
                "special_id"=>$_POST["special_id"],
                "order_quantity"=>$_POST["number"]])){
                return true;
            }else{
                return false;
            }
        }else{
            if( Db::table("specialty")->where("park_user_id=".Cookie::get("id")." and special_id=".$_POST["special_id"])->update([
                "order_quantity"=>$_POST["number"]+$array["order_quantity"]])){
                return true;
            }else{
                return false;
            }
        }
    }
    //对商品的总价格进行计算
    public function shop(){
        if(Cookie::has("username")){
            $name=Cookie::get("username")." <a href='".url('Index/cancel')."'>注销</a>";
        }else{
            $name=' <a href="'.url('Index/login').'">登录&注册</a>';
        }


        $get = Cookie::get("id") ;//  用户ID
        $zongjia=0;
        $time=time();
        $list = Db::table('specialty')->field("special_id,order_quantity")->where('park_user_id',$get)->select();
        for($aa=0;$aa<count($list);$aa++){
            $list[$aa]["shangping"]=Db::table('special')->field("name,amounts")->where('id',$list[$aa]["special_id"])->find();
            $list[$aa]["shangping"]["xiaoji"]=$list[$aa]["order_quantity"]* $list[$aa]["shangping"]["amounts"];
            $zongjia+= $list[$aa]["shangping"]["xiaoji"];
        }

        $this->assign('list', $list);
        $this->assign('zongjia', $zongjia);
        $this->assign('time', $time.($get));
        return $this->fetch('specialty/specshop',["username"=>$name]);//
    }
    public function shopbay(){
        Db::startTrans();
        if(Db::table("dingdanbiao")->insert(["park_user_id"=>Cookie::get("id"),"dingdanhao"=>$_POST["dingdanhao"],"zongjia"=>$_POST["zongjia"]])){
            if(Db::table("specialty")->where("park_user_id=".Cookie::get("id"))->delete()){
                DB::commit();
                return true;
            }else{
                DB::rollback();
                return false;
            }
        }else{
            DB::rollback();
            return false;
        }
    }

}






